﻿using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Fetches.Fetch.PagePool.Consume
{
    public class QueueProcessCoordinator : IProcessCoordinator
    {
        private HashSet<int> _set=new HashSet<int>();
        private ConcurrentQueue<int> _queue = new ConcurrentQueue<int>();

        public void Initialize(string config, long uniqueId)
        {
            //队列型协调员不需要额外初始化
        }

        public bool IsEmpty()
        {
            return _queue.IsEmpty;
        }

        public void EnqueueWhenNoDuplicate(int pageIndex)
        {
            lock (_set)
            {
                if (!_set.Contains(pageIndex))
                {
                    _set.Add(pageIndex);
                    _queue.Enqueue(pageIndex);
                }
            }


        }

        public int Dequeue()
        {
            if (_queue.TryDequeue(out int result))
            {
                return result;
            }

            return -1;
        }
    }
}
